package com.bocai.controller;

import com.bocai.bean.TSalesOutMoneyInfo;
import com.bocai.service.IDistributorCashInfoService;
import com.bocai.service.ISalesService;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.ObjUtils;
import com.bocai.utils.RandomUtils;
import com.bocai.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("sales")
public class SaleController {

    @Autowired
    private ISalesService salesService;
    @Autowired
    private IDistributorCashInfoService iDistributorCashInfoService;

    @ApiOperation("二级分销商获取金钱预览")
    @RequestMapping(value = "/moneyPriview",method = RequestMethod.POST)
    @ApiImplicitParam(paramType = "query", name = "saleId", dataType = "int", required = true, value = "分销商Id", defaultValue = "")
    @ResponseBody
    public ResponseData moneyPriview(Integer saleId){
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{saleId});
        if (ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        data.setResults(salesService.selectSalesDetails(saleId));

        return data;
    }

    @ApiOperation("二级分销商获取会员列表")
    @RequestMapping(value = "/userList",method = RequestMethod.POST)
    @ApiImplicitParam(paramType = "query", name = "saleId", dataType = "int", required = true, value = "分销商Id", defaultValue = "")
    @ResponseBody
    public ResponseData getUserList(Integer saleId){
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{saleId});
        if (ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("salesId", saleId);
        data.setResults(salesService.selectSalesUserList(map));

        return data;
    }

    @ApiOperation("二级分销商获取会员充值明细")
    @RequestMapping(value = "/userDetail",method = RequestMethod.POST)
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "会员Id", defaultValue = ""),
    })
    @ResponseBody
    public ResponseData getUserDetail(Integer userId){
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userId});
        if (ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        data.setResults(salesService.selectSalesDetail(userId));

        return data;
    }

    @ApiOperation("二级分销商获取提现记录")
    @RequestMapping(value = "/withdrawList",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "saleId", dataType = "int", required = true, value = "分销商Id", defaultValue = ""),
    })
    @ResponseBody
    public ResponseData getWithdrawList(Integer saleId){
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{saleId});
        if (ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        data.setResults(salesService.selectOutMoney(saleId));

        return data;
    }

    @ApiOperation(value = "分销商提现钱数", notes = "分销商提现钱数")
    @RequestMapping(value = "/saveDistributorCashInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData saveCashInfo(Integer distributorId, BigDecimal money, String payString) {
        ResponseData data = new ResponseData();
        if (null == distributorId || distributorId < 0) {
            data.setCodeEnum(CodeEnum.CODE_90002);
        } else {
            TSalesOutMoneyInfo cashInfo = new TSalesOutMoneyInfo();
            cashInfo.setDistributorId(distributorId);
            String orderNum = RandomUtils.getOrderNumber();
            cashInfo.setCashNumber(orderNum);
            cashInfo.setCashMoney(money);
            cashInfo.setPaymentType(payString);
            cashInfo.setCashState(1);
            cashInfo.setCreateTime(new java.util.Date());
            cashInfo.setCashArrivalMoney(new BigDecimal(0));
            int num = iDistributorCashInfoService.insertSelective(cashInfo);
            if (num == 1) {
                data.setCodeEnum(CodeEnum.SUCCESS);
            } else if (num == 2) {
                data.setCodeEnum(CodeEnum.CODE_10009);
            } else if (num == 3) {
                data.setCodeEnum(CodeEnum.CODE_90005);
            } else {
                data.setCodeEnum(CodeEnum.CODE_10003);
            }
        }
        return data;
    }
}
